package com.scalar.forecast.projectSetup.actionCommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.projectSetup.dao.UserDefinedFieldsDAO;

/**
 * Processes request to save user defined field( of type list) values
 *
 * Created by Veeru. 
 * Date: Feb 14, 2005
 * Time: 2:25:07 PM 
 */


public class UserDefinedListValuesSaveCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "userDefinedLisValues" );

        boolean success = false;
        int projectId   = 0;
        String listName = null;
        DBTransaction dbTransaction = null;
        HashMap errors = new HashMap ();
        HashMap info = new HashMap ();
        ArrayList userDefinedListValuesList = null;

        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );

        String userDefinedListValues[] = request.getParameterValues("value");

        if ( request.getParameter( "projectId" ) != null && request.getParameter( "listName" ) != null ) {
            try {
                projectId = Integer.parseInt( request.getParameter("projectId").trim() );
                listName      = request.getParameter( "listName" ).trim();
                dbTransaction = DBTransaction.getInstance();
                dbTransaction.begin();
                UserDefinedFieldsDAO.saveUserDefinedListValues( dbTransaction, "PROJSETUP", projectId, listName, userDefinedListValues );
                success = true;

            }catch( Exception e ){
                e.printStackTrace();
            }
            finally {
                if ( success ) {
                    try {
                        dbTransaction.commit();
                        info.put( INFO_USER_DEFINED_FIELD_VALUES_SAVED_SUCCESSFULLY, null);
                    }catch ( Exception e){
                        e.printStackTrace();
                    }
                }else {
                    try {
                        dbTransaction.rollback();
                    }catch ( Exception e ) {
                        e.printStackTrace();
                    }
                    errors.put( ERROR_COULD_NOT_SAVE_USER_DEFINED_FIELD_VALUES, null);
                }
            }
        }
        try {
            userDefinedListValuesList = UserDefinedFieldsDAO.loadUserDefinedListValues( null, projectId, "PROJSETUP", listName );
        }catch ( Exception e ){
            errors.put("Error while loading the User Defined Field values", null );
        }
        if ( userDefinedListValuesList != null ) {
            request.setAttribute("list", userDefinedListValuesList );
        }else {
            request.setAttribute("list", new ArrayList() );
        }
        return actionForward;
    }
}
